<?php
/**
 * @version             0.1
 * @package             JImageLib
 * @author 		Daniel Ghilea <danutz@gmail.com>
 * @copyright   Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license             GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );
jimport( 'JImageLib.JImageLib' );


class plgContentJimagelib extends JPlugin {

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param object $subject The object to observe
	 * @param object $params  The object that holds the plugin parameters
	 * @since 1.5
	 */
        function plgContentJimagelib( &$subject, $params )
        {
                parent::__construct( $subject, $params );
        }
	
                
        function onBeforeContentSave( &$article, $isNew )
        {
                global $mainframe;
                
                              
                if ( JString::strpos( $article->introtext, 'image' ) === false && JString::strpos( $article->introtext, 'img' ) === false &&JString::strpos( $article->fulltext, 'image' ) === false && JString::strpos( $article->fulltext, 'img' ) === false) {
                        return true;
                }
                
                // expression for {image}, <img /> tags and <img /> images
                $regex = '/\&lt;img\s*[^>]*?\&gt;|{image\s*[^}]*?}|<img[^>]*?>/i';

                // searching the introtext for matches
                preg_match_all( $regex, $article->introtext, $matches );

      
                
                $count = count( $matches[0] );

                if ( $count ) {
                        $this->_process( $article, $matches, $count, $regex);
                }
                
                
                // searching the fulltext for matches
                preg_match_all( $regex, $article->fulltext, $matches );
				
                $count = count( $matches[0] );

                if ( $count ) {
                        $this->_process( $article, $matches, $count, $regex);
                }
                
                
                return true;
        }
        
        
        
        
        protected function _process( &$article, &$matches, $count, $regex)
        {
        		global $mainframe;
        		               
        		for ( $i=0; $i < $count; $i++ )
                {
                        $load = str_replace( '{image', '', $matches[0][$i] );
                        $load = str_replace( '<img', '', $load );
                        $load = str_replace( '&lt;img', '', $load );
                        $load = str_replace( '}', '', $load );
                        $load = str_replace( '/>', '', $load );
                        $load = str_replace( '>', '', $load );
                        $load = str_replace( '/&gt;', '', $load );
                        $load = str_replace( '&gt;', '', $load );
                        $load = trim( $load );

                        $img = new JImageLib();
                        if (! $tag = $img->parse($load)){
                        	$msg =  'JImageLib Plug-In :: ' . $img->getError();
							$mainframe->redirect('index.php?option=com_content&task=edit&cid[]='.$article->id, $msg, 'error');
                        }
                  		                       	                       	 
                        $article->introtext = preg_replace( '{'.$matches[0][$i].'}', $tag, $article->introtext );
                        $article->fulltext = preg_replace( '{'.$matches[0][$i].'}', $tag, $article->fulltext );
                }
         }
        
}


?>